package com.eightywork.life.adapter;

import java.util.List;

import com.eightywork.life.R;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.PmView;
import com.eightywork.life.service.CacheService;
import com.eightywork.life.service.DataService;
import com.eightywork.life.util.FuncUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PmDetailAdapter extends BaseAdapter {

	private int pdResource = R.layout.item_pm_detail;
	private LayoutInflater pdInflater;
	private Context pdContext;
	private ImageLoader imageLoader;
	private List<PmView> list;

	public PmDetailAdapter(Context context, List<PmView> list) {
		pdContext = context;
		pdInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
		
		this.list = list;
		// TODO Auto-generated constructor stub
	}
	
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return (list != null)?list.size():0;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		PmView pmView = list.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = pdInflater.inflate(pdResource, parent, false);
			holder.toll = (LinearLayout)convertView.findViewById(R.id.llLeft);
			holder.fromll = (LinearLayout)convertView.findViewById(R.id.llRight);
			holder.time = (TextView)convertView.findViewById(R.id.tvTime);
			holder.toAvatar = (ImageView)convertView.findViewById(R.id.ivLeftAvatar);
			holder.toVerifyType = (ImageView)convertView.findViewById(R.id.ivLeftVip);
		//	holder.toTime = (TextView)convertView.findViewById(R.id.tvLeftTime);
			holder.toMsg = (TextView)convertView.findViewById(R.id.tvLeftContent);
			holder.fromAvatar = (ImageView)convertView.findViewById(R.id.ivRightAvatar);
			holder.fromVerifyType = (ImageView)convertView.findViewById(R.id.ivRightVip);
		//	holder.fromTime = (TextView)convertView.findViewById(R.id.tvRightTime);
			holder.fromMsg = (TextView)convertView.findViewById(R.id.tvRightContent);
			holder.pgBar = (ProgressBar)convertView.findViewById(R.id.pdRightSending);
			holder.ivFail = (ImageView)convertView.findViewById(R.id.ivRightSendFail);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		holder.pgBar.setVisibility(View.INVISIBLE);
		holder.ivFail.setVisibility(View.INVISIBLE);
		holder.time.setText(FuncUtil.timeToText(Long.valueOf(pmView.getDateline()) * 1000));
		if (pmView.getMsgfromuid().equals(CacheService.getSpace().getUid())) {
			holder.toll.setVisibility(View.GONE);
			holder.fromll.setVisibility(View.VISIBLE);
		//	holder.fromTime.setText(FuncUtil.timeToText(Long.valueOf(pmView.getDateline()) * 1000));
			imageLoader.DisplayImage(pmView.getMsgfromavatar(), holder.fromAvatar, 0, 0, 0);
			holder.fromVerifyType.setBackgroundResource(DataService.getVerifyTypeRes
						(pmView.getMsgfromverifyType()));		
			holder.fromMsg.setText(pmView.getMessage());
			final String state = pmView.getState();
			if (state != null && state.equals("sending")) {
				holder.pgBar.setVisibility(View.VISIBLE);
				holder.ivFail.setVisibility(View.INVISIBLE);
			} else if (state != null && state.equals("fail")) {
				holder.pgBar.setVisibility(View.INVISIBLE);
				holder.ivFail.setVisibility(View.VISIBLE);
			}
			
		} else {
			holder.toll.setVisibility(View.VISIBLE);
			holder.fromll.setVisibility(View.GONE);
		//	holder.toTime.setText(FuncUtil.timeToText(Long.valueOf(pmView.getDateline()) * 1000));	
			holder.toMsg.setText(pmView.getMessage());
			imageLoader.DisplayImage(pmView.getMsgfromavatar(), holder.toAvatar, 0, 0, 0);
			holder.toVerifyType.setBackgroundResource(DataService.getVerifyTypeRes
					(pmView.getMsgfromverifyType()));
		}	
		
		return convertView;
	}


	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}
	public final class ViewHolder {
		private LinearLayout toll;
		private LinearLayout fromll;
		private TextView time;
		private ImageView toAvatar;
		private ImageView toVerifyType;
	//	private TextView toTime;
		private TextView toMsg;
		private ImageView fromAvatar;
		private ImageView fromVerifyType;
	//	private TextView fromTime;
		private TextView fromMsg;
		private ProgressBar pgBar;
		private ImageView ivFail;
		
	}

}
